package ducna.android.aoeclip.screen.category;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.DrawerLayout;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.LinearLayout;

import ducna.android.aoeclip.R;
import ducna.android.aoeclip.common.controller.BaseActivity;
import ducna.android.aoeclip.common.utils.Constant;
import ducna.android.aoeclip.common.utils.Logger;
import ducna.android.aoeclip.screen.category.adapter.ArticleFragmentPagerAdapter;
import ducna.android.aoeclip.screen.setting.SettingActivity;


/**
 * Created by DUCNA on 11/4/2014.6
 */
public class CategoryActivity extends BaseActivity implements Constant, CategoryFragment.NavigationDrawerCallbacks, ViewPager.OnPageChangeListener {
    public static final String TAG = CategoryActivity.class.getSimpleName();

    private DrawerLayout mDrawerLayout;
    private LinearLayout mLinearDrawer;
    private ViewPager mViewPager;
    private boolean isShowDrawer = false;

    private ArticleFragmentPagerAdapter mAdapterPager;
    private CategoryFragment mCategoryFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setActionBarIcon(R.drawable.ic_drawer);
        findViewById();
//        initView();
        initView();
    }

    public void findViewById() {
        mDrawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout);
        mLinearDrawer = (LinearLayout) findViewById(R.id.drawer);
        mViewPager = (ViewPager) findViewById(R.id.viewPager);
        mCategoryFragment = (CategoryFragment) getSupportFragmentManager().findFragmentById(R.id.fragment_category);

    }


    public void initData() {
        initProgressDialog();
    }

    public void initView() {
        Bundle bundle = mCategoryFragment.getArguments();

        mAdapterPager = new ArticleFragmentPagerAdapter(getSupportFragmentManager(), bundle);
        mViewPager.setAdapter(mAdapterPager);
        mViewPager.setOnPageChangeListener(this);

    }


    @Override
    protected int getResID() {
        return R.layout.activity_category;
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.toolbar_menu, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        switch (id) {
            case R.id.action_setting:
                Intent intent = new Intent(CategoryActivity.this, SettingActivity.class);
                startActivity(intent);
                return true;
            case android.R.id.home:
                Logger.out(TAG, "home");
                // open drawer
                if (isShowDrawer) {
                    mDrawerLayout.closeDrawer(mLinearDrawer);
                    isShowDrawer = !isShowDrawer;
                    setActionBarTitle(getResources().getString(R.string.app_name));
                } else {
                    mDrawerLayout.openDrawer(mLinearDrawer);
                    isShowDrawer = !isShowDrawer;
                    setActionBarTitle(getResources().getString(R.string.select_category));
                }
                return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onNavigationDrawerItemSelected(int position) {

    }

    @Override
    public void onPageScrolled(int i, float v, int i2) {

    }

    @Override
    public void onPageSelected(int i) {

    }

    @Override
    public void onPageScrollStateChanged(int i) {

    }
}
